<template>
  <div class="app-container">
    <el-row>
      <el-form ref="form" :model="queryParams" label-width="80px">
        <el-col :span="6">
          <el-form-item label="主体名称">
            <el-input v-model="queryParams.subjectName" size="small"
                      placeholder="请输入主体名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="法人姓名">
            <el-input v-model="queryParams.legalPersonName" size="small"
                      placeholder="请输入法人姓名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="注册手机号码" label-width="120px">
            <el-input v-model="queryParams.phone" size="small"
                      placeholder="请输入注册手机号码"></el-input>
          </el-form-item>
        </el-col>
        <template v-if="toggleContent">
        <el-col :span="6" >
          <el-form-item label="是否有营业执照"  label-width="120px">
            <el-select v-model="queryParams.hasBusinessLicense" size="small"
                       placeholder="请选择是否有营业执照" style="width: 250px">
              <el-option label="有" :value="0"></el-option>
              <el-option label="没有" :value="1"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        </template>
        <template v-if="toggleContent">
        <el-col :span="6">
          <el-form-item label="所属行业">
            <el-input v-model="queryParams.industry" size="small"
                      placeholder="请输入所属行业"></el-input>
          </el-form-item>
        </el-col>
        </template>
        <template v-if="toggleContent">
        <el-col :span="6">
          <el-form-item label="生产类型">
            <el-input v-model="queryParams.productionType" size="small"
                      placeholder="请输入生产类型"></el-input>
          </el-form-item>
        </el-col>
      </template>
        <template v-if="toggleContent">
        <el-col :span="6">
          <el-form-item label="证件号码">
            <el-input v-model="queryParams.idNumber" size="small"
                      placeholder="请输入证件号码"></el-input>
          </el-form-item>
        </el-col>
        </template>
        <el-col :span="6">
          <el-form-item label-width="0" style="margin-left: 20px">
            <el-button type="primary" icon="el-icon-search" size="small"
                       style="margin-right: 10px" @click="getPage">搜索</el-button>
            <el-button type="primary" icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>

            <a style="margin-left: 10px;color: #3a8ee6"
               @click="toggleContentHandle">
              {{toggleContent ? '收起' : '展开'}}
              <i v-if="toggleContent" class="el-icon-arrow-up"></i>
              <i v-else class="el-icon-arrow-down"></i>
            </a>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-button type="primary" icon="el-icon-download" size="medium"
                   style="margin-right: 10px" @click="handleExport">导出</el-button>
        <el-button type="danger" icon="el-icon-delete" size="medium">批量删除</el-button>
      </el-col>
    </el-row>
    <el-row style="margin-top: 20px">
      <el-col :span="24">
        <!--border:当布尔属性默认值为false时，显式的使用该属性，就表示设置该属性为true-->
        <el-table :data="tableData" style="width: 100%" border>
          <el-table-column type="selection" width="100" align="center"></el-table-column>
          <el-table-column label="是否有营业执照" align="center">
            <template slot-scope="scope">
              <el-tag type="danger" v-if="scope.row.hasBusinessLicense== 1">无</el-tag>
              <el-tag type="success" v-else-if="scope.row.hasBusinessLicense == 0">有</el-tag>
              <el-tag type="info" v-else>错误</el-tag>
            </template>
          </el-table-column>

          <el-table-column prop="subjectName" label="主体名称" width="280"
                           align="center"></el-table-column>
          <el-table-column prop="legalPersonName" label="法人姓名" width="180"
                           align="center"></el-table-column>
          <el-table-column prop="idNumber" label="证件号码" align="center" width="200">
          </el-table-column>
          <el-table-column prop="phone" label="注册手机号码" align="center" width="120">
          </el-table-column>
          <el-table-column prop="detailAddress" label="详细地址" align="center" width="300">
          </el-table-column>
          <el-table-column prop="productionType" label="生产类型" align="center">
          </el-table-column>
          <el-table-column prop="industry" label="所属行业" align="center">
          </el-table-column>
          <el-table-column prop="registeredCapital" label="注册资金" align="center">
          </el-table-column>
          <el-table-column prop="establishTime" label="成立时间" align="center" width="120"></el-table-column>

          <el-table-column fixed="right" label="操作" width="200px" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="small"
                         @click="openDialog(scope.row)"  style="margin-right: 8px">编辑</el-button>
<!--              <el-button type="text" size="small" style="color:red" @click="deleteByUserId(scope.row.id)">删除</el-button>-->
              <el-dropdown @command="handleCommand">
                <el-button type="primary" size="small">
                  更多<i class="el-icon-arrow-down el-icon--right"></i>
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item :command="{action: 'edit', row: scope.row}">审核</el-dropdown-item>
                  <el-dropdown-item :command="{action: 'edit', row: scope.row}">重置</el-dropdown-item>
                  <el-dropdown-item :command="{action: 'delete', row: scope.row}">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <el-dialog title="编辑" :visible.sync="dialogVisible" width="30%">
      <!--添加表单组件-->
      <el-form ref="updateForm" :model="editObjects" label-width="100px">
        <el-form-item label="是否有营业执照">
          <el-select v-model="editObjects.hasBusinessLicense" size="small" placeholder="请选择用
户状态" style="width: 240px">
            <el-option label="有" :value="0"></el-option>
            <el-option label="无" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主体名称">
          <el-input v-model="editObjects.subjectName" placeholder="请输入主体名
称"></el-input>
        </el-form-item>
        <el-form-item label="法人姓名">
          <el-input v-model="editObjects.legalPersonName" placeholder="请输入法人姓名">
          </el-input>
        </el-form-item>
        <el-form-item label="证件号码">
          <el-input v-model="editObjects.idNumber" placeholder="请输入证件号码">
          </el-input>
        </el-form-item>
        <el-form-item label="注册手机号码">
          <el-input v-model="editObjects.phone" placeholder="请输入注册手机号">
          </el-input>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input v-model="editObjects.detailAddress" placeholder="请输入详细地址"></el-input>
        </el-form-item>
        <el-form-item label="生产类型">
          <el-input v-model="editObjects.productionType" placeholder="请输入生产类型">
          </el-input>
        </el-form-item>
        <el-form-item label="所属行业">
          <el-input v-model="editObjects.industry" placeholder="请输入所属行业"></el-input>
        </el-form-item>
        <el-form-item label="注册时间">
          <el-input v-model="editObjects.establishTime" placeholder="请输入成立时
间"></el-input>
        </el-form-item>
        <el-form-item label="注册资金">
          <el-input v-model="editObjects.registeredCapital" placeholder="请输入注册资金">
          </el-input>
        </el-form-item>
      </el-form>
      <!--对话框底部按钮组，固定代码结构-->
      <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitData">确 定</el-button>
      </span>
    </el-dialog>

    <el-row>
      <el-col :span="24" style="margin-top: 20px" align="center">
        <el-pagination layout="prev, pager, next"
                       @current-change="getPageByPageNum"
                       :page-size="queryParams.pageSize"
                       :total="total"
                       :current-page="queryParams.pageNum"></el-pagination>
      </el-col>
    </el-row>

    <!-- 弹窗区域：一般用于新增或编辑弹窗-->

  </div>
</template>
<script>
import {listNjtUserInfo,updateNjtUserInfo,deleteNjtUserInfo} from "@/api/njt/user/register";

export default {
  name: "XXX",
  dicts:['njt_member_flag','njt_perfect_flag'],
  data() {
    return {
      /** 查询条件 */
      queryParams: {
        pageNum: 1,
        pageSize: 5,
        subjectName: "",     // 对应表单中的 companyName
        phone: "",           // 修改这里，与表单绑定字段一致
        hasBusinessLicense: "",
        userStatus: ""       // 添加缺失的 userStatus 字段
      },
      /** 切换搜索区域内容 */
      toggleContent: false, // 默认隐藏，值为true时显示
      /** 数据总条数 */
      total: 56,
      /** 编辑对话框控制 */
      dialogVisible: false,// 默认不显示对话框
      /** 编辑对话框绑定对象 */
      editObjects: {
        hasBusinessLicense: '', // 是否有营业执照：1（有）、0（没有）
        subjectName: '', // 主体名称
        legalPersonName: '', // 法人姓名
        idNumber: '', // 证件号码
        phone: '', // 联系电话
        detailAddress: '', // 详细地址
        productionType: 0, // 生产类型选择索引
        industry: 0, // 所属行业选择索引
        registeredCapital: '', // 注册资金
        establishTime: '', // 成立时间
      }
    }

  },
  created() {
    this.getPage()
  },
  methods: {
    //删除按钮操作
    deleteByUserId(userId) {
      this.$modal.confirm('是否确认删除用户编号为"' + userId + '"的数据项？').then(function () {
        return deleteNjtUserInfo(userId)
      }).then(() => {
        this.getPage()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
        this.$modal.msgError("删除失败")
      })
    },
    //编辑弹窗确定
    submitData() {
      updateNjtUserInfo(this.editObjects).then(response => {
        //刷新表格数据
        this.getPage()
        //弹窗提示
        this.$modal.msgSuccess(response.msg)
        //关闭弹窗
        this.dialogVisible = false
      })
    },
    //分页页码切换
    getPageByPageNum(num) {
      this.queryParams.pageNum = num;
      this.getPage();
    },
    // 分页查询
    getPage() {
      // 过滤空值参数，避免不必要的查询条件
      const params = {};
      Object.keys(this.queryParams).forEach(key => {
        if (this.queryParams[key] !== "" && this.queryParams[key] !== undefined) {
          params[key] = this.queryParams[key];
        }
      });

      listNjtUserInfo(params).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
      }).catch(error => {
        this.$modal.msgError("数据加载失败");
        console.error("getPage error:", error);
      });
    },
    /** 切换搜索内容显示/隐藏方法 */
    toggleContentHandle() {
      this.toggleContent = !this.toggleContent
    },
    /** 打开编辑对话框方法 */
    openDialog(objData) {
      Object.assign(this.editObjects, objData)
      this.dialogVisible = true;
    },
    handleExport() {
      this.download('/nongjt/userInfo/export', {
        ...this.queryParams
      }, `会员注册信息_${new Date().getTime()}.xlsx`)
    },
resetQuery() {
  this.queryParams = {
    pageNum: 1,
    pageSize: 5,
    companyName: "",
    phone: "",
    hasBusinessLicense: "",
    userStatus: ""
  };
  this.getPage();
},
handleCommand(command) {
  switch (command.action) {
    case 'audit':
      // 处理审核逻辑
      break;
    case 'reset':
      // 处理重置逻辑
      break;
    case 'delete':
      this.deleteByUserId(command.row.id);
      break;
    default:
      break;
  }
}

  }
}
</script>
